from django.db import models
import django.utils.timezone as timezone
from goods.models import GoodsInfo
# Create your models here.


def get_order_number():
    import random, time
    from datetime import datetime
    res1 = str(random.randint(0, 10) * 10)
    res2 = str(random.randint(0, 10) * 10)
    res3 = str(int(time.time()))
    return res1 + res2 + res3


class Auth(models.Model):
    ID = models.AutoField(primary_key = True)
    openID = models.CharField(max_length=100,default="")
    nickname = models.CharField(max_length=50,default="",verbose_name="微信昵称")
    phoneNumber = models.CharField(max_length=22,default="",verbose_name="手机号")
    shou_address = models.TextField(default="[]",verbose_name="收获地址")
    cart = models.OneToOneField("Cart",default=ID,verbose_name="购物车",on_delete=models.CASCADE)

    def __str__(self):
        return self.openID

class Order(models.Model):
    BACK1_RESULT_CHOICES = (
        ("1", "线下付款"),
        ("2", "线上支付"),
    )
    paymentStatus_Choices = (
        ("0","待付款"),
        ("1","已付款")
    )

    DeliveryStatus_Choices = (
        ("0","待发货"),
        ("1","已发货")
    )
    harvestStatus_Choices = (
        ("0","待收货"),
        ("1","已收货")
    )

    orderResult_Choices = (
        ("0","已取消"),
        ("1","正在进行中")
    )


    orderID = models.AutoField(primary_key = True)
    orderNumber = models.TextField(default="",verbose_name="订单号")
    userID = models.ForeignKey(Auth,default="[]",verbose_name="用户信息",on_delete=models.PROTECT)
    # shouDddress = models.OneToOneField(userID.shou_address,verbose_name="收获地址",on_delete=models.PROTECT)
    showAddress = models.TextField(default="[]",verbose_name="收获地址")
    goodsInfo = models.TextField(default="",verbose_name="货物信息")
    totalPrice = models.TextField(default="",verbose_name="总价")
    payGoodsNumber = models.CharField(default="",max_length=100,verbose_name="购买的商品总个数")
    paymentStatus = models.CharField(max_length=100,choices=paymentStatus_Choices,blank=True,verbose_name="付款状态")
    DeliveryStatus = models.CharField(max_length=100,choices=DeliveryStatus_Choices,blank=True,verbose_name="发货状态")
    harvestStatus = models.CharField(max_length=100,choices=harvestStatus_Choices,blank=True,verbose_name="收获状态")
    orderResult = models.CharField(max_length=100,default="1",verbose_name="订单进度结果",choices=orderResult_Choices)
    buyerAnnocate = models.TextField(default="",verbose_name="买家备注")
    paymentTime = models.CharField(max_length=100,default="",verbose_name="付款时间")
    joinTime = models.DateTimeField(verbose_name="订单生成时间",default = timezone.now)
    paymentMode = models.CharField(max_length=100,choices=BACK1_RESULT_CHOICES,blank=True,verbose_name="付款方式")


class Cart(models.Model):
    cartID = models.AutoField(primary_key = True)
    goodsInfo1 = models.TextField(blank=True,verbose_name="商品")


